/*
 * Project it.battlehorse.rcp.tools.actors.sampledb
 * Created on Dec 2, 2005
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * A copy of the LGPL is available also at
 * http://www.gnu.org/copyleft/lesser.html
 * 
 */ 
package it.battlehorse.rcp.tools.actors.sampledb.view;

import it.battlehorse.rcp.sl.annot.Injected;
import it.battlehorse.rcp.sl.annot.Serviceable;
import it.battlehorse.rcp.tools.actors.IActorRunner;
import it.battlehorse.rcp.tools.actors.sampledb.SampleDbActor;
import it.battlehorse.rcp.tools.dbbase.DatasourceConfig;
import it.battlehorse.rcp.tools.dbbase.IDbManager;

import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.IMemento;
import org.eclipse.ui.IViewSite;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.forms.widgets.FormToolkit;
import org.eclipse.ui.forms.widgets.ScrolledForm;
import org.eclipse.ui.part.ViewPart;

/**
 * A view which contains an input field where the user can type a database query. The actor associated to
 * this view will perform the query and count the number of results once started.
 * 
 * @author battlehorse
 * @since Dec 2, 2005
 */
@Serviceable
public class SampleDBView extends ViewPart {
	
	/**
	 * The view id , whose value is {@value }.
	 */
	public static final String ID_VIEW = "it.battlehorse.rcp.tools.actors.sampledb.view.SampleDBView";
	
	private FormToolkit toolkit;
	
	private ScrolledForm form;
	
	/* The widget which contains the query typed by the user */
	private Text t;
	
	/* The list of available datasources */
	private TableViewer lv;

	/* The last query typed by the user */
	private String query;
	
	/* The db manager */
	private IDbManager manager;
	
	/* The actor runner */
	private IActorRunner runner;
	
	/**
	 * Injects a db manager into this object
	 * 
	 * @param manager the db manager
	 */
	@Injected
	public void setDbManager(IDbManager manager) {
		this.manager = manager;
	}
	
	/**
	 * Injects an actor runner into this object
	 *  
	 * @param runner the runner
	 */
	@Injected 
	public void setActorRunner(IActorRunner runner) {
		this.runner = runner;
	}
	
	@Override public void init(IViewSite site, IMemento memento) throws PartInitException {
		super.init(site,memento);
		
		if (memento != null)
			query = memento.getString("QUERY");
		else
			query = "";
	}	
	
	@Override public void saveState(IMemento memento) {
		memento.putString("QUERY",t.getText());
	}

	/* (non-Javadoc)
	 * @see org.eclipse.ui.part.WorkbenchPart#createPartControl(org.eclipse.swt.widgets.Composite)
	 */
	@Override
	public void createPartControl(Composite parent) {
		toolkit = new FormToolkit(parent.getDisplay());
		form = toolkit.createScrolledForm(parent);
		
		form.setText("Sample Database Actor");
		
		GridLayout layout = new GridLayout(2,false);
		form.getBody().setLayout(layout);
		GridData gd;
		
		Label l = toolkit.createLabel(form.getBody(),"This Actor is designed to test " +
				"database connectivity and provide a sample of how to handle " +
				"retrieval of connections from a  datasource.", SWT.WRAP);
		
		gd = new GridData();
		gd.horizontalSpan = 2;
		l.setLayoutData(gd);
		
		l = toolkit.createLabel(form.getBody(),"Type a simple SELECT query to test" +
				"that the connection works correctly.", SWT.WRAP);
		
		gd = new GridData();
		gd.horizontalSpan = 2;
		l.setLayoutData(gd);

		toolkit.createLabel(form.getBody(),"Datasource:");
		
		lv = new TableViewer(form.getBody(), SWT.SINGLE | SWT.BORDER | SWT.V_SCROLL);
		manager.addDatasourceViewer(lv); // implicitly sets input and providers
		
		new TableColumn(lv.getTable(),SWT.LEFT).setText("Datasources");
        
		for (TableColumn col : lv.getTable().getColumns()) 
			 col.pack();
		
		gd = new GridData();
		gd.widthHint = 200;
		gd.heightHint = 100;
		lv.getControl().setLayoutData(gd);
		
		toolkit.createLabel(form.getBody(),"SELECT Query:");
		t = toolkit.createText(form.getBody(),query);
		t.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		
		Button b = toolkit.createButton(form.getBody(), "Click to run the query", SWT.PUSH | SWT.FLAT);
		gd = new GridData();
		gd.horizontalSpan = 2;
		b.setLayoutData(gd);		
		b.addSelectionListener(runner.newButtonActorLauncher(SampleDbActor.ID_ACTOR));
		
		toolkit.paintBordersFor(form.getBody());
	}

	/* (non-Javadoc)
	 * @see org.eclipse.ui.part.WorkbenchPart#setFocus()
	 */
	@Override
	public void setFocus() {
		t.setFocus();

	}
	
	@Override
	public void dispose() {
		manager.removeDatasourceViewer(lv);
		toolkit.dispose();
		super.dispose();
	}
	
	/**
	 * Returns the selected datasource
	 *
	 * 
	 * @return datasource
	 */
	public DatasourceConfig getSelectedDatasource() {
		IStructuredSelection sel = (IStructuredSelection) lv.getSelection();
		if (sel != null && sel.getFirstElement() != null) {
			return (DatasourceConfig) sel.getFirstElement();
		}
		else 
			return null;
	}
	
	/**
	 * Returns the query submitted by the user
	 * 
	 * @return the query submitted by the user
	 */
	public String getQuery() {
		return t.getText();
	}

}
